import _ from 'lodash'
import exParser from './exParser'
import customStyle from './customStyle'

let _rulesRequired = { required: true, message: `必填项`, trigger: ['blur', 'change'] }
export default {
  data() {
    return {
      existMain: true,
      rules: [],
      propertyNameArr: []
    }
  },
  mixins: [exParser, customStyle],
  computed: {
    isDetailMode() {
      return this.getParsedEx('isDetailMode')()
    },
    required() {
      return this.getParsedEx('required')()
    },
    exist() {
      if (!this.existMain) {
        return false
      }
      if (!this.config.isExistExp && this.config.isExistExp !== false) {
        return true
      }
      return !!this.getParsedEx('isExistExp')()
    },
    readonly() {
      if (this.config.inputWay === 'syncLink' || this.config.inputWay === 'asyncLink') {
        return true
      }
      if (this.readonlyMain) {
        return true
      }
      if (!this.config.isReadonlyExp) {
        return false
      } else {
        return !!this.getParsedEx('isReadonlyExp')()
      }
    },
    show() {
      if (!this.config.isShowExp && this.config.isShowExp !== false) {
        return true
      }
      return !!this.getParsedEx('isShowExp')()
    },

  },
  watch: {
    // 'config.propertyName': function(newVal, oldVal) {
    //   this.propertyNameArr = []
    //   _.forEach(this.rootForm.sortable, one => {
    //     this.getAllPropertyName(one)
    //   })
    //   if (_.groupBy(this.propertyNameArr)[newVal].length > 1) {
    //     this.$set(this.rootForm.properties, newVal, this.rootForm.properties[newVal] || this.default)
    //   } else {
    //     this.$set(this.rootForm.properties, newVal, this.rootForm.properties[oldVal] || this.default)
    //   }
    //   if (!_.groupBy(this.propertyNameArr)[oldVal] || !_.groupBy(this.propertyNameArr)[oldVal].length) {
    //     delete this.rootForm.properties[oldVal]
    //   }
    // },
    // 'config.required': function(value) {
    //   this.getRequiredRules()
    // }
  },
  methods: {
    // getRequiredRules() {
    //   if (!this.readonlyMain) {
    //     let newValue = this.getParsedEx('required')()
    //     if (!newValue) {
    //       _.remove(this.rules, one => one === _rulesRequired)
    //     } else {
    //       this.rules.push(_rulesRequired)
    //     }
    //     this.existMain = false
    //     this.$nextTick(() => {
    //       this.existMain = true
    //     })
    //   }
    // },
    getAllPropertyName(option) {
      if (option.category === 'layout' && option.config.containers.length > 0) {
        _.forEach(option.config.containers, one => {
          if (one.children.length > 0) {
            _.forEach(one.children, child => {
              this.getAllPropertyName(child)
            })
          }
        })
      }
      if (option.config.propertyName) {
        this.propertyNameArr.push(option.config.propertyName)
      }
    }
  },
  created() {
    // 是否必填
    if (!this.readonlyMain) {
      if (this.getParsedEx('required')()) {
        this.rules.push(_rulesRequired)
      }
      this.$watch(this.getParsedEx('required'), (newValue) => {
        if (!newValue) {
          _.remove(this.rules, one => one === _rulesRequired)
        } else {
          this.rules.push(_rulesRequired)
        }
        this.existMain = false
        this.$nextTick(() => {
          this.existMain = true
        })
      })
    }
    // 组件初始值
    if ('propertyName' in this.config) {
      let val = this.rootForm.properties[this.config.propertyName]
      delete this.rootForm.properties[this.config.propertyName]
      this.$set(this.rootForm.properties, this.config.propertyName, val === undefined ? this.default : val)
    }
  }
}
